package com.example.administrator.teainfo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.teainfo.R;
import com.example.administrator.teainfo.utils.ImageDownloadHelper;

import java.util.List;
import java.util.Map;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2016/12/10.
 */

public class MyListViewAdapter extends BaseAdapter {
    private List<Map<String,Object>> list;
    private Context context;
    public MyListViewAdapter(Context context,List<Map<String,Object>> list){
        Log.e(TAG, "MyListViewAdapter: "+"eeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
        this.context = context;
        this.list = list;
    }
    public void addList(List<Map<String,Object>> list){
        this.list.addAll(list);
    }
    @Override
    public int getCount() {
        Log.e(TAG, "getCount: list.size()"+list.size());
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        Log.e(TAG, "getItemId: position"+position);
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.e(TAG, "getView: "+"gtfffffff" );
        final ViewHolder mhHolder;
        if (convertView == null) {
            mhHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.list_item_layout, null);
            mhHolder.title = (TextView) convertView
                    .findViewById(R.id.title);
            mhHolder.source = (TextView) convertView
                    .findViewById(R.id.source);
            mhHolder.nickname = (TextView) convertView
                    .findViewById(R.id.nickname);
            mhHolder.create_time = (TextView) convertView
                    .findViewById(R.id.create_time);
            mhHolder.wap_thumb = (ImageView) convertView
                    .findViewById(R.id.wap_thumb);
            convertView.setTag(mhHolder);
            Log.e(TAG, "getView: "+"你好");
        } else {
            mhHolder = (ViewHolder) convertView.getTag();
            Log.e(TAG, "getView: "+"你好");
        }
        Log.e(TAG, "getView: "+"你好");

        String title = (String) list.get(position).get("title");
        Log.e(TAG, "getView: title"+title);
        String source = (String) list.get(position).get("source");
        String create_time = (String) list.get(position).get("create_time");
        String nickname = (String) list.get(position).get("nickname");
        String wap_thumb = list.get(position).get("wap_thumb").toString();
        mhHolder.title.setText(title);
        mhHolder.source.setText(source);
        mhHolder.create_time.setText(create_time);
        mhHolder.nickname.setText(nickname);
        // mhHolder.wap_thumb.setImageResource(R.drawable.ic_launcher);
			/* 判断要不要给图片留位置 */
        if (wap_thumb == null || wap_thumb.equals("")) {
            mhHolder.wap_thumb.setVisibility(View.GONE);
        } else {
            mhHolder.wap_thumb.setTag(wap_thumb);//设置标签
//            mhHolder.wap_thumb.setVisibility(View.INVISIBLE);
        }
        ImageDownloadHelper helper = new ImageDownloadHelper();
        helper.myDownloadImage(context, wap_thumb, mhHolder.wap_thumb, new ImageDownloadHelper.OnImageDownloadListener() {
            @Override
            public void onImageDownload(Bitmap bitmap, String imgUrl) {
                View view = mhHolder.wap_thumb.findViewWithTag(imgUrl);
                if (view instanceof ImageView && bitmap!=null) {
                    mhHolder.wap_thumb.setImageBitmap(bitmap);
                    mhHolder.wap_thumb.setVisibility(View.VISIBLE);
                }
                notifyDataSetChanged();
            }
        });
        //图片缓存lru
//        if (cacheImageMap.get(wap_thumb) == null) {
//				/* 不存在开始请求网络，并设置图片 */
//            new MyTask(mhHolder.wap_thumb, wap_thumb, 1).execute(wap_thumb);
//        } else {
//            mhHolder.wap_thumb.setImageBitmap(cacheImageMap.get(wap_thumb));
//            mhHolder.wap_thumb.setVisibility(View.VISIBLE);
//        }
        return convertView;
    }

    class ViewHolder {
        private TextView title;
        private TextView source;
        private TextView nickname;
        private TextView create_time;
        private ImageView wap_thumb;
    }
}
